package com.chrhc.projects.cpub.gen;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.chrhc.projects.cpub.gen.codegenerate.GenerateCode;
import com.chrhc.projects.cpub.gen.def.CodeResourceUtil;
import com.chrhc.projects.cpub.gen.util.CreateBean;
import com.chrhc.projects.cpub.gen.xml.IbatisConfigBean;
import com.chrhc.projects.cpub.gen.xml.IbatisConfigXMLReader;

import freemarker.template.TemplateException;

public class IbatisConfigGenerate
{
    static Logger log = Logger.getLogger(IbatisConfigGenerate.class);
    
    private CreateBean createBean;
    
    private IbatisConfigBean config = null;
    
    private String version = null;
    
    private String author = null;
    
    private String path = System.getProperty("user.dir");
    
    public IbatisConfigGenerate(String configName, String author, String version)
    {
        this.version = version;
        this.author = author;
        CodeResourceUtil.AUTHOR = author;
        CodeResourceUtil.VARSION = version;
        String configFile = path + "/" + configName;
        config = IbatisConfigXMLReader.getIbatisconfig(configFile);
        createBean = new CreateBean();
        String url = config.getConnectionURL();
        String username = config.getUserId();
        String password = config.getPassword();
        createBean.setMysqlInfo(url, username, password);
    }
    
    public void generateCode()
    {
        log.debug("generateCode GenerateCode.class.getClassLoader().getResource::"
            + GenerateCode.class.getClassLoader().getResource(""));
        GenerateCode generate = new GenerateCode(config);
        try
        {
            log.debug("generateCode generate::" + generate);
            generate.codeGenerateByIbatisConfig();
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("sql Exception:" + e.toString());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("IOException Exception:" + e.toString());
        }
        catch (TemplateException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("TemplateException Exception:" + e.toString());
        }
    }
    
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        String configName = "/src/main/resource/conf/generatorConfig-testuser.xml";
        // IbatisConfigGenerate gen = new IbatisConfigGenerate(configName);
        // gen.generateCode();
    }
    
}
